@file:Suppress("DEPRECATION")

package com.even.camerarecoder

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.Camera
import android.view.Surface
import com.evendai.loglibrary.Timber


object CameraHelper {

    /** 打开前置摄像头  */
    fun openFacingFrontCamera(): Camera? {
        val cameraInfo = Camera.CameraInfo()
        val numberOfCameras = Camera.getNumberOfCameras()
        for (i in 0 until numberOfCameras) {
            Camera.getCameraInfo(i, cameraInfo)
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return Camera.open(i)
            }
        }
        return Camera.open()
    }

    /** 打开后置摄像头，与Camera.open()函数功能相同  */
    fun openFacingBackCamera(): Camera? {
        val numberOfCameras = Camera.getNumberOfCameras()
        val cameraInfo = Camera.CameraInfo()
        for (i in 0 until numberOfCameras) {
            Camera.getCameraInfo(i, cameraInfo)
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                Timber.i("后置摄像头id为：$i")
                return Camera.open(i)
            }
        }
        return Camera.open()
    }

    @Suppress("unused")
    fun printCameraInfo(context: Context) {
        val hasCamera = checkCameraHardware(context)
        Timber.i(if (hasCamera) "有摄像头" else "没有摄像头")
        Timber.i("摄像头个数：" + Camera.getNumberOfCameras())
        val cameraInfo = Camera.CameraInfo()
        Camera.getCameraInfo(0, cameraInfo)
        Timber.i("用摄像头0是：" + (if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) "前置摄像头" else "后置摄像头"))
        Timber.i("摄像头0的图像方向：" + cameraInfo.orientation)

        val frontCamera = openFacingFrontCamera() ?: throw RuntimeException("打开前置摄像头失败")
        val parameters = frontCamera.parameters
        Timber.i("照像机支持的预览大小：")
        for (mSize in parameters.supportedPreviewSizes) {
            Timber.i(mSize.width.toString() + " x " + mSize.height + ", ")
        }
        Timber.i("照像机支持的图片大小：")
        for (mSize in parameters.supportedPictureSizes) {
            Timber.i(mSize.width.toString() + " x " + mSize.height + ", ")
        }

        Timber.i("照像机支持的预览格式：" + parameters.supportedPreviewFormats + ", 842094169就是ImageFormat.YV12，17就是ImageFormat.NV21")
        Timber.i("照像机支持的图片格式：" + parameters.supportedPictureFormats + ", 256就是ImageFormat.JPEG")
        Timber.i("照像机支持的焦点模式：" + parameters.supportedFocusModes)
        Timber.i("照像机支持预览的帧速率：" + parameters.supportedPreviewFrameRates)
        Timber.i("照像机支持的闪存模式：" + parameters.supportedFlashModes)

        Timber.i("所有参数：" + parameters.flatten())
        frontCamera.release()
    }


    /** 检查设备是否提供摄像头  */
    @Suppress("MemberVisibilityCanBePrivate")
    fun checkCameraHardware(context: Context) = context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)

    @Suppress("unused")
    fun getCameraDisplayOrientation(activity: Activity, cameraId: Int): Int {
        val info = Camera.CameraInfo()
        Camera.getCameraInfo(cameraId, info)
        val rotation = activity.windowManager.defaultDisplay.rotation
        var degrees = 0
        when (rotation) {
            Surface.ROTATION_0 -> degrees = 0
            Surface.ROTATION_90 -> degrees = 90
            Surface.ROTATION_180 -> degrees = 180
            Surface.ROTATION_270 -> degrees = 270
        }

        var result: Int
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360
            result = (360 - result) % 360  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360
        }
        return result
    }

}